<?php
/**
 * Created by PhpStorm.
 * User: miidi
 * Date: 2018/7/12
 * Time: 18:04
 */

namespace app\api\controller;


use app\api\model\Banner;
use app\api\model\House;
use app\api\model\Test;
use app\api\validate\Idistenger;

class HouseCon
{
    /**web 展示
     * @url getHouseList
     *   筛选
     */
    public function getHouseList()
    {
        $house = new House();
        $page = input('page');
        $pageSize = input('pageSize');
        $keyword = input('key');
        $houseNew = input('houseNew');
        $quyu = input('quyu');
        $mianji = input('mianji');
        $totalPrice = input('totalPrice');
        $price_gt = 0;
        $price_lt = 1000000000;
        if ($totalPrice) {
            $arr = explode('-', $totalPrice);
            $price_gt = intval($arr[0]);
            $price_lt = intval($arr[1]);
        }
        $mianji_gt = 0;
        $mianji_lt = 1000;
        if ($mianji) {
            $arr = explode('-', $mianji);
            $mianji_gt = intval($arr[0]);
            $mianji_lt = intval($arr[1]);
        }
        $huxing = input('huxing');
        $query1 = $house->where('houseNew', $houseNew)
            ->where('quxian', 'like', '%' . $quyu . '%')
            ->where('totalPrice', ['>=', $price_gt], ['<=', $price_lt], 'and')
            ->where('AreaStr', ['>=', $mianji_gt], ['<=', $mianji_lt], 'and')
            ->where('Community|description', 'like', '%' . $keyword . '%','OR')
            ->where('jifang', 'like', '%' . $huxing . '%')
            ->order('update_time desc');
        $result = $query1->paginate($pageSize, false, ['page' => $page]);
//        $house = House::where(1)->limit(10)->order('update_time desc')->select();
        $output = [
            'code' => 0,
            'data' => $result,
        ];
        return json($output);

    }

    public function smallHouseList()
    {
        $house = new House();
        $get_data = input('type');
        $huxing = input('huxing');
        $id = input('id');
        $result = [];
        if (intval($get_data) === 1) {
            $result = $house->where('type', intval($get_data))->order('update_time desc')->limit(10)->select();
        } else if ($get_data === 'like') {
            $result = $house->where('jifang', $huxing)
                ->where('id', 'neq', $id)
                ->order('update_time desc')->limit(6)->select();
        }

        $output = [
            'code' => 0,
            'data' => $result,
        ];
        return json($output);
    }

    /**
     * 后台展示  分页处理
     * @url getHouseAllList
     */
    public function getHouseAllList()
    {
        $page = input('page');
        $pageSize = input('pageSize');
        $keyword = input('filterTitle');
        if ($keyword) {
            /**
             * --------模糊查询----------
             * $words = "吃";
             * $where['title'] = array('like','%'.$words.'%');
             * $list = $TagDB->where($where)->select();
             */
            $query1 = House::where('Community', 'like', '%' . $keyword . '%');
            $output = $query1->paginate($pageSize, false, ['page' => $page]);
            return json($output);
        } else {
            $query = House::where(1)->order('update_time desc');
            $output = $query->paginate($pageSize, false, ['page' => $page]);
            return json($output);
        }

    }

    /**
     * @url getHouseDetails
     */
    public function getHouseDetails($id)
    {
        (new Idistenger())->goCheck();
        $housebanner = Banner::where('type_house_id', $id)->select();
        $details = House::getById($id);
        $output = [
            'code' => 0,
            'data' => [
                'banner' => $housebanner,
                'details' => $details
            ],
        ];
        return json($output);
    }

    public function test()
    {
        $banner = new Banner();
        $result = $banner::get(12);
//    $result->desc='think';
//    $result->save();
        return $result;
    }

    /**
     *
     * @return \think\response\Json
     * @throws \think\exception\DbException
     */
    public function addHouseHist()
    {
        header('Content-type:text/html; Charset=utf-8');
        $data = $_POST['formData'];
        if (!array_key_exists('id', $data)) {
            $house = new House();
            $data['id'] = null;
            $result = $house->save($data);
            if ($result) {
                return json([
                    'code' => 0,
                    'message' => '新增成功',
                ]);
            }
        } else {
            $house = House::get($data['id']);
            $result = $house->save($data);
            if ($result) {
                return json([
                    'code' => 0,
                    'message' => '更新成功',
                ]);
            }
        }
    }

    public function deleteCurrent($id)
    {
        /**
         * User::destroy(1);
         * // 支持批量删除多个数据
         * User::destroy('1,2,3');
         * // 或者
         * User::destroy([1,2,3]);
         */
        $id = input('id');
        $result = House::destroy($id);
        //return json($result);
        if ($result) {
            return json([
                'code' => 0,
                'messaeg' => 'ok',
                'data' => '删除成功！'
            ]);
        }
    }

}